package com.hdjd.controller;

import com.hdjd.entities.ShopCar;
import com.hdjd.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄徵岚
 * @create 2021/1/28 11:10
 */
@RequestMapping("/shopCart")
@Controller
public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;

    @ResponseBody
    @RequestMapping(value = "addOrUpdateShopCartByAccIdAndGoId",method = RequestMethod.PUT)
    public Map<String,Object> addOrUpdateShopCartByAccIdAndGoId(Integer goId,Integer accId){
        Map<String,Object> map = new HashMap<>();
        boolean flag;
        try{
            flag = this.shopCartService.addOrUpdateShopCartByAccIdAndGoId(goId,accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "添加购物车失败");
            return map;
        }
        if (flag){
            map.put("code", 1001);
            map.put("msg", "添加购物车成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "添加购物车失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/editShopGoodsNum",method = RequestMethod.PUT)
    public Map<String,Object> editShopGoodsNum(Integer shopCarId,Integer goNum){
        Map<String,Object> map = new HashMap<>();
        int flag;
        try{
            flag = this.shopCartService.editShopCartBuyNumByShopCartId(shopCarId, goNum);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "修改购物车数量失败");
            return map;
        }
        if (flag != 0){
            map.put("code", 1001);
            map.put("msg", "修改购物车数量成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "修改购物车数量失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/deleteShopCartByShopCartId",method = RequestMethod.DELETE)
    public Map<String,Object> deleteShopCartByShopCartId(Integer shopCarId){
        Map<String,Object> map = new HashMap<>();
        int flag;
        try{
            flag = this.shopCartService.deleteShopCartByShopCartId(shopCarId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "删除购物车失败");
            return map;
        }
        if (flag != 0){
            map.put("code", 1001);
            map.put("msg", "删除购物车成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "删除购物车失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/deleteShopCartByShopCartIdString",method = RequestMethod.DELETE)
    public Map<String,Object> deleteShopCartByShopCartIdString(String shopCarIdString){
        Map<String,Object> map = new HashMap<>();
        int flag;
        try{
            flag = this.shopCartService.deleteShopCartByShopCartIdString(shopCarIdString);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "删除多个购物车失败");
            return map;
        }
        if (flag != 0){
            map.put("code", 1001);
            map.put("msg", "删除多个购物车成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "删除多个购物车失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/selectShopCartByAccId",method = RequestMethod.GET)
    public Map<String,Object> selectShopCartByAccId(Integer accId){
        Map<String,Object> map = new HashMap<>();
        List<ShopCar> shopCars;
        try{
            shopCars = this.shopCartService.selectShopCartByAccId(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询购物车失败");
            return map;
        }
        if (shopCars != null){
            map.put("code", 1001);
            map.put("msg", "查询购物车成功");
            map.put("shopCars",shopCars);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "查询购物车失败");
            return map;
        }
    }
}
